/**
 * Created by liuqingxia on 2016/6/22.
 */


/**
 * 查找相匹配的表名
 */
function searchTableName() {

    clickSearchTableNameBtn("reader");
    clickSearchTableNameBtn("writer");
    searchTableNameInputChange("reader");
    searchTableNameInputChange("writer");

    clickRelatedTableNameLi("reader");
    clickRelatedTableNameLi("writer");
}

/**
 * 查找相匹配的表名
 * @param target
 */
function clickSearchTableNameBtn(target) {

    $(document).on("click", "#" + target + "SearchTableBtn", function () {

        var tableName = $.trim($("#" + target + "SearchTableName").val());

        triggerTableNameLi(target, tableName);
    });
}

/**
 * 触发TableNameLiSelect
 * @param tableName
 */
function triggerTableNameLi(target, tableName) {

    var findMatchTableName = 0;//0代表没有找到相匹配的表，1代表找到相匹配的表

    $.each($("li." + target + "-tableName-li"), function () {

        var tableNameSpan = $(this).find("span.text");
        var eachTableName = $.trim(tableNameSpan.html());

        tableName = tableName.toLowerCase();
        eachTableName = eachTableName.toLowerCase();

        if (tableName == eachTableName) {
            findMatchTableName = 1;
            try {
                $(this).trigger("click");
            } catch (e) {

            }
            return false;
        }

        return true;
    });

    if (0 == findMatchTableName) {
        swal("提示", "没有找到匹配的表名", "error");
    }
}


/**
 *searchTableNameInput输入值变化
 */
function searchTableNameInputChange(target) {

    $("#" + target + "SearchTableName").bind('input propertychange', function () {

        var inputTableName = $.trim($(this).val()).toLowerCase() == "" ? 0 : $.trim($(this).val()).toLowerCase();
        var matchLiTableNames = [];

        $.each($("li." + target + "-tableName-li"), function () {

            var tableNameSpan = $(this).find("span.text");
            var eachTableName = $.trim(tableNameSpan.html());

            if (eachTableName.toLowerCase().contains(inputTableName)) {
                matchLiTableNames.push(eachTableName);
            }

            return true;

        });

        showRelatedTableNames(matchLiTableNames, target);
    });
}

/**
 * 输入框内容变化时，展示相关联的tableNames
 * @param matchLiTableNames
 */
function showRelatedTableNames(matchLiTableNames, target) {

    var length = matchLiTableNames.length;

    if (0 == length) {
        hideSearchTableNamesLiDiv(target);
        return;
    }

    var lis = "";

    for (var i = 0; i < length; i++) {

        lis += "" +
        "<li class='" + target + "-searchTableName-li'>" +
        "<a> " +
        "<span class = 'text'>" +
        matchLiTableNames[i] +
        "</span> " +
        "<span class= 'glyphicon glyphicon-ok check-mark'>" +
        "</span>" +
        "</a>" +
        "</li>";
    }

    appendSearchTableNamesLiDiv(lis, target);
    showSearchTableNamesLiDiv(target);

    return;
}


function appendSearchTableNamesLiDiv(lis, target) {
    var relatedTableNamesLi = $("#" + target + "RelatedTableNamesLi");
    relatedTableNamesLi.empty();
    relatedTableNamesLi.append(lis);
}

function hideSearchTableNamesLiDiv(target) {
    var relatedTableNamesLiDiv = $("#" + target + "RelatedTableNamesLiDiv");
    relatedTableNamesLiDiv.hide();
}

function showSearchTableNamesLiDiv(target) {
    var relatedTableNamesLiDiv = $("#" + target + "RelatedTableNamesLiDiv");
    relatedTableNamesLiDiv.show();
}

function clickRelatedTableNameLi(target) {
    $(document).on("click", "." + target + "-searchTableName-li", function () {
        var tableName = $.trim($(this).find("span.text").html());
        triggerTableNameLi(target, tableName);
        hideSearchTableNamesLiDiv(target);
    });
}
